package com.common.base.widget

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.LinearLayout
import com.common.base.R
import com.common.base.app.fragment.BaseDialogFragment
import com.common.base.app.extras.dpDimenPx
import com.common.base.app.extras.no
import com.common.base.databinding.BaseDialogLoadingBinding

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/19
 * <p>
 * 描述：加载中对话框
 * <p>
 * 修订历史：
 */
class LoadingDialog : BaseDialogFragment<BaseDialogLoadingBinding>() {
    companion object {
        fun newInstance(content: String, mCancelable: Boolean): LoadingDialog {
            val dialog = LoadingDialog()
            dialog.content = content
            dialog.mCancelable = mCancelable
            return dialog
        }
    }

    override fun getViewBinding() = BaseDialogLoadingBinding.inflate(layoutInflater)

    private var content: String? = ""
    private var mCancelable: Boolean = true
    override fun getSelfWidth(): Int = ViewGroup.LayoutParams.WRAP_CONTENT
    override fun getSelfHeight(): Int = dpDimenPx(R.dimen.dp_80).toInt()
    override fun getStyle(): Int = STYLE_NORMAL
    override fun getCancelable(): Boolean = mCancelable

    override fun initView(view: View) {
        setShowPositionCenter()
        content.isNullOrEmpty().no {
            mViewBinding.loadingMsg.text = content
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.MyDialog)
    }


    override fun onStart() {
        super.onStart()
        val window = dialog?.window
        val lp = window?.attributes
        lp?.dimAmount = 0f
        lp?.flags = lp?.flags?.or(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        window?.attributes = lp
    }
}